﻿using System;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.Runtime;

namespace BasicTools
{
    class CreateCirclesTools : AutoCADBase
    {
        private SelectTools selectTools;

        public void CreatCircles()
        {
            selectTools = new SelectTools();
        }

        public void CreateEntity()
        {
            //
            //ObjectId[] ids = selectTools.SelectDBTexts();
            //
            ObjectId[] ids = selectTools.SelectBlocks();
            if (ids == null || ids.Length < 1) return;

            using (Transaction trans = _db.TransactionManager.StartTransaction())
            {
                foreach (ObjectId id in ids)
                {
                    //将测图块转换成圆
                    BlockReference block = trans.GetObject(id, OpenMode.ForRead) as BlockReference;
                    Circle circle = new Circle(block.Position, new Vector3d(0, 0, 1), 1);

                    //为了林研将测图文字转换成圆
                    //DBText text = trans.GetObject(id, OpenMode.ForRead) as DBText;
                    //Circle circle = new Circle(text.Position, new Vector3d(0, 0, 1), 1);

                    BlockTable bt = _db.BlockTableId.GetObject(OpenMode.ForRead) as BlockTable;
                    BlockTableRecord btr = trans.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;
                    btr.AppendEntity(circle);
                    trans.AddNewlyCreatedDBObject(circle, true);
                }
                trans.Commit();
                _ed.WriteMessage("\n转换圆成功！");
            }
        }

        public void ChangeToElevation()
        {
            //
            //ObjectId[] ids = selectTools.SelectCircles();
            //
            ObjectId[] ids = selectTools.SelectBlocks();
            if (ids == null || ids.Length < 1) return;

            PromptDoubleResult pdr = _ed.GetDouble("请输入绘图水位");
            if (pdr.Status != PromptStatus.OK)
            {
                _ed.WriteMessage("输入有误！");
                return;
            }
            double chartLever = pdr.Value;

            //为了满足林研要求，以文字为对象
            //using (Transaction trans = db.TransactionManager.StartTransaction())
            //{
            //    foreach (ObjectId id in ids)
            //    {
            //        Circle circle = trans.GetObject(id, OpenMode.ForWrite) as Circle;
            //        double zElevation = chartLever - circle.Center.Z;
            //        Point3d newPosition = new Point3d(circle.Center.X, circle.Center.Y, zElevation);
            //        circle.Center = newPosition;

            //        circle.DowngradeOpen();
            //    }
            //    trans.Commit();
            //    ed.WriteMessage("\n水深转换高程成功！");
            //}

            //以块为对象
            using (Transaction trans = _db.TransactionManager.StartTransaction())
            {
                foreach (ObjectId id in ids)
                {
                    BlockReference blockRef = trans.GetObject(id, OpenMode.ForWrite) as BlockReference;
                    double zElevation = chartLever - blockRef.Position.Z;
                    Point3d newPosition = new Point3d(blockRef.Position.X, blockRef.Position.Y, zElevation);
                    blockRef.Position = newPosition;

                    AttributeReference attRef1 = blockRef.AttributeCollection[0].GetObject(OpenMode.ForWrite) as AttributeReference;
                    double newAtt1 = Convert.ToDouble(attRef1.TextString);
                    newAtt1 = Math.Truncate(Math.Round(zElevation, 1));
                    attRef1.TextString = Convert.ToString(newAtt1);
                    attRef1.ColorIndex = 27;

                    AttributeReference attRef2 = blockRef.AttributeCollection[1].GetObject(OpenMode.ForWrite) as AttributeReference;
                    double newAtt2 = Convert.ToDouble(attRef2.TextString);
                    newAtt2 = Math.Round((Math.Round(zElevation, 1) - newAtt1) * 10);
                    attRef2.TextString = Convert.ToString(newAtt2);
                    attRef2.ColorIndex = 27;

                    blockRef.DowngradeOpen();
                }
                trans.Commit();
                _ed.WriteMessage("\n水深转换高程成功！");
            }
        }
    }
}
